package org.jtb.quickr;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Logger;
import org.jdom.Element;

/**
 * Groups response.
 *
 * @see org.jtb.quickr.PeopleService#getPublicGroups(String)
 */
public class GroupsResponse extends FlickrResponse {
    private static Logger logger = Logger.getLogger(GroupsResponse.class.getName());
    
    /**
     * Groups group response.
     */
    public static class Group extends FlickrResponse {
        private String nsId;
        private String name;
        private boolean admin;
        private boolean eighteenPlus;
        
        Group(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            this.admin = getAttrBoolean(e, "admin");
            this.eighteenPlus = getAttrBoolean(e, "eighteenplus");
            
            this.nsId = e.getAttributeValue("nsid");
            this.name = e.getAttributeValue("name");
        }

        public String getNsId() {
            return nsId;
        }

        public String getName() {
            return name;
        }

        public boolean isAdmin() {
            return admin;
        }

        public boolean isEighteenPlus() {
            return eighteenPlus;
        }
    }
    
    private List<Group> groups;
    
    GroupsResponse(InputStream is) throws QuickrException {
        init(is);
    }
    
    protected void populate(Element e) throws QuickrException {
        Element groupsElement = e.getChild("groups");
        
        groups = new ArrayList<Group>();
        List<Element> groupElements = groupsElement.getChildren("group");
        if (logger.isDebugEnabled()) {
            logger.debug("number of groups: " + groupElements.size());
        }
        for (Element groupElement: groupElements) {
            Group g = new Group(groupElement);
            groups.add(g);
        }
    }

    public List<Group> getGroups() {
        return Collections.unmodifiableList(groups);
    }
}
